// pages/chart/chart.js
import Charts from "../../utils/wxchart.js";
import { data } from "./chart.data.js";
var app = getApp();
Page({
  data: data,
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      options: options
    })
    if (options.type == 7) {
      wx.setNavigationBarTitle({
        title: '近期体温',
      })
    } else if (options.type == 8) {
      wx.setNavigationBarTitle({
        title: '近期体重',
      })
    } else if (options.type == 10) {
      wx.setNavigationBarTitle({
        title: '身体数据',
      })
    }
    if (options.type == 10) {
      let that = this;
      getTypeData(that);
      return false;
    } else {
      var that = this;
      getDataList(that, options.id);
    }
  },
  onShareAppMessage: function () {
    if (this.data.options.type == 7) {
      var title = '快来膜拜我的近期体温变化吧'
    } else if (this.data.options.type == 8) {
      var title = '快来膜拜我的近期体重变化吧'
    } else if (this.data.options.type == 10) {
      var title = '快来膜拜我的近期数据变化吧'
    }
    if (this.data.options.type == 10) {
      return {
        title: title,
      }
    } else {
      return {
        title: title,
      }
    }
  },
  //切换图表
  checkChart: function (e) {
    let type = e.currentTarget.dataset.key;
    this.setData({
      'chartset.selected': type
    })
    let data = this.data.Xdata[type];
    let categories = this.data.categories;
    let that = this;
    let chartset = that.data.chartset[type];
    draw(that, categories, data, chartset);
  }
})

//根据 type、patientid 获取数据
function getTypeData(that) {
  let options = that.data.options;
  let params = {
    type: options.type,
    patientId: options.patientid,
    start: 0,
    size: 5
  }
  app.https.get(that, '/aiding-web/api/camps', params, function (rel) {
    if (rel.length > 0) {
      that.setData({
        console: false
      })
      let categories = [],
        data = {
          weight: [],
          bmi: [],
          waist: [],
          whr: []
        };
      for (let i in rel) {
        rel[i].detail = JSON.parse(rel[i].detail);
        categories.push(rel[i].createtime);
        data.weight.push(rel[i].detail.weight);
        data.bmi.push(rel[i].detail.bmi);
        data.waist.push(rel[i].detail.waist);
        data.whr.push(rel[i].detail.whr);
      };
      categories = dateSet(categories)
      that.setData({
        categories: categories,
        Xdata: data
      })
      let set = {
        name: '体重',
        unit: 'kg'
      }
      draw(that, categories, data.weight, set);
    } else {
      that.setData({
        console: true
      })
    }
  })
}

//获取 chart 需要的数据，计算 chart 的一些配置参数
function getDataList(that, id) {
  var url = "/aiding-web/api/camp/" + id;
  app.https.get(that, url, { days: 5 }, function (data) {
    var xData = [], //x轴标注
      categories = []; //关键点的数据
    for (var i = 0; i < data.length; i++) { //数组重新排序
      data[i].detail = JSON.parse(data[i].detail);
      var now = new Date(data[i].createtime);
      if (String(now) == 'Invalid Date') { //ios日期显示 bug
        now = new Date(data[i].createtime.replace(/-/g, '/'));
      }
      var year = now.getYear();
      var month = now.getMonth() + 1;
      var date = now.getDate();
      categories.push(month + "-" + date);
      if (that.data.options.type == 8) {
        xData.push(Number(data[i].detail.weight));
      } else if (that.data.options.type == 7) {
        xData.push(Number(data[i].detail.temperature));
      }
    }
    categories.reverse();
    xData.reverse();
    draw(that, categories, xData);
  });
}

//绘制 chart
function draw(that, categories, seriesData, params) {
  try { //计算 chart 的宽度高度
    var chartWidth = wx.getSystemInfoSync().windowWidth * 0.9;
  } catch (e) {
    // Do something when catch error
    var chartWidth = 280;
  };
  if (that.data.options.type == 8) {
    var name = '体重';
    var unit = 'kg';
  } else if (that.data.options.type == 7) {
    var name = '基础体温';
    var unit = '℃';
  } else {
    var name = params.name;
    var unit = params.unit;
  }
  new Charts({
    canvasId: 'chart',
    type: 'line',
    categories: categories,
    // extra: {
    //   lineStyle: 'curve',
    // },
    series: [{
      name: name,
      data: seriesData,
      format: function (val) {
        return val + unit;
      }
    }],
    xAxis: {
      disableGrid: true,
      fontColor: '#000000',
      fontSize: 14
    },
    yAxis: {
      title: '体重 (kg)',
      format: function (val) {
        return val.toFixed(2);
      },
      gridColor: '#ffffff',
      titleFontColor: '#ffffff',
      fontColor: '#ffffff',
    },
    width: chartWidth,
    height: chartWidth * 0.8
  })
}
//日期处理
function dateSet(timeArray) {
  let newtimeArry = [];
  for (let i in timeArray) {
    let time = new Date(timeArray[i]);
    if (String(time) == 'Invalid Date') { //ios日期显示 bug
      time = new Date(timeArray[i].replace(/-/g, '/'));
    }
    let month = time.getMonth() + 1;
    let day = time.getDate();
    newtimeArry.push(month + '-' + day);
  }
  return newtimeArry;
}